/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;


import entities.Event;
import entities.Prize;
import entities.Registration;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.aptech.session.beans.EventFacade;
import org.aptech.session.beans.PrizeFacade;
import org.aptech.session.beans.RegistrationFacade;
import org.aptech.session.beans.WinnerFacade;

/**
 *
 * @author Chien
 */
@ManagedBean
@SessionScoped
public class PrizeControl {
    @EJB
    private EventFacade eventFacade;
    @EJB
    private PrizeFacade prizeFacade;
    @EJB
    private WinnerFacade winnerFacade;
    @EJB
    private RegistrationFacade registrationFacade;
    private Prize prize;
    private Event event;
    /**
     * Get List<Prize> in a month ago.
     * @return 
     */
    public List<Prize> prizeListWithinAmonth(){      
        
        return prizeFacade.getPrizeListWinthinAmonth();
    }
    public Registration selectRegistrationByPrize(Prize prize){
        
        return null;
    }
    

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Prize getPrize() {
        return prize;
    }

    public void setPrize(Prize prize) {
        this.prize = prize;
    }
    
    

    /**
     * Creates a new instance of PrizeControl
     */
    public PrizeControl() {
        prize=new Prize();
       
    }
    public List<Prize> getAllPrize()
    {
        return prizeFacade.findAll();
    }
    public void insert()
   {
        try{
            prizeFacade.create(prize);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Create Success", ""));
            
        }catch(Exception ex)
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Insert failed", ""));
        }
             
   }
   public void edit()
   {
        try {
            prizeFacade.edit(prize);
            FacesContext context = FacesContext.getCurrentInstance();
             
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/PrizeManager.xhtml");
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Update failed", ""));
        }
                
   }
   public void delete(Prize prize)
   {
        try {
            prizeFacade.remove(prize);
            FacesContext context = FacesContext.getCurrentInstance();
             
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/PrizeManager.xhtml");
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Delete failed", ""));
        }
               
   }
   public void goInsert()
   {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
             
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/PrizeInsert.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(PrizeControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       
   }
   public void showDetail(Prize prize)
   {
       
         this.prize=prize;
        try {
          
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/PrizeUpdate.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(PrizeControl.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
   public javax.faces.model.SelectItem[] getEventList()
   {
       SelectItem[] options=null;
       List<Event> events=eventFacade.findAll();
       if (events != null && events.size() > 0) {
            int i = 0;
            options = new SelectItem[events.size()];
            
            for (Event eventlist : events) {
                options[i++] = new SelectItem(eventlist, eventlist.getTitle().substring(0, 11));
            }
        }
        return options;
   }
   public javax.faces.model.SelectItem[] getPrizeList()
   {
       SelectItem[] options=null;
       List<Prize> prizes=prizeFacade.findAll();
       if (prizes != null && prizes.size() > 0) {
            int i = 0;
            
            options = new SelectItem[prizes.size()];
            for (Prize prizelist : prizes) {
                options[i++] = new SelectItem(prizelist, prizelist.getTitle());
            }
            
        }
        return options;
   }
   
}
